package com.suarte.core;

import com.suarte.utils.Persistent;
import org.appfuse.model.BaseObject;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
 * @date   Dec 30, 2010
 * @author Ggutierrez
 */
@Entity
@Table(name = "sa_companies")
public class Company extends BaseObject implements Persistent {

    private Long id;
    private String description;
    private String address;
    private String phone;
    private String fax;
    private String email;
    private String website;
    private Integer creditDays;
    private Float balance = 0f;
    private Float balanceLocal = 0f;
    private Long oldId;

    public Company() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "COMPANY_SEQ")
    @SequenceGenerator(name = "COMPANY_SEQ", sequenceName = "COMPANY_SEQ", allocationSize = 1)
    public Long getId() {
        return id;
    }

    @Column(name = "description", length = 150)
    public String getDescription() {
        return description;
    }

    @Column(name = "address", length = 250)
    public String getAddress() {
        return address;
    }

    @Column(name = "phone", length = 100)
    public String getPhone() {
        return phone;
    }

    @Column(name = "fax", length = 100)
    public String getFax() {
        return fax;
    }

    @Column(name = "email", length = 100)
    public String getEmail() {
        return email;
    }

    @Column(name = "website", length = 100)
    public String getWebsite() {
        return website;
    }

    @Column(name = "credit_days")
    public Integer getCreditDays() {
        return creditDays;
    }

    @Column(name = "balance")
    public Float getBalance() {
        return balance;
    }

    @Column(name = "balance_local")
    public Float getBalanceLocal() {
        return balanceLocal;
    }

    @Column(name = "old_id")
    public Long getOldId() {
        return oldId;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setFax(String fax) {
        this.fax = fax;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public void setCreditDays(Integer creditDays) {
        this.creditDays = creditDays;
    }

    public void setBalance(Float balance) {
        this.balance = balance;
    }

    public void setBalanceLocal(Float balanceLocal) {
        this.balanceLocal = balanceLocal;
    }

    public void setOldId(Long oldId) {
        this.oldId = oldId;
    }
    

    public void credit(Float credit, Float creditLocal) {
        balance = (balance != null ? balance : 0) - (credit != null ? credit : 0);;
        balanceLocal = (balanceLocal != null ? balanceLocal : 0) + (creditLocal != null ? creditLocal : 0);
    }

    public void debit(Float debit, Float debitLocal) {
        balance = (balance != null ? balance : 0) + (debit != null ? debit : 0);
        balanceLocal = (balanceLocal != null ? balanceLocal : 0) + (debitLocal != null ? debitLocal : 0);
    }

    @Override
    public String toString() {
        return description;
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if (!(other instanceof Company)) {
            return false;
        }
        Company _i = (Company) other;
        return id.equals(_i.id);
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }
}
